﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using SimpleOrmCFPC.Core.Model;
using SimpleOrmCFPC.Core.Query.Requests;
using SimpleOrmCFPC.Core.Query.expressions.ExprssionCompilers.Helpers;

namespace SimpleOrmCFPC.Core.Query.expressions.ExprssionCompilers
{
    public class Link
    {
        public PropertyInfo Field;
        public Type Table;
    }

    public class MembrAccessBuilder
    {
        public static List<Link> BuildLinks(MemberExpression mem)
        {
            var links = new List<Link>();
            if (mem.Expression != null)
            {
                if (mem.Expression is MemberExpression)
                {
                    links.AddRange(BuildLinks(mem.Expression as MemberExpression));
                }
                if (mem.Expression.NodeType == ExpressionType.MemberAccess)
                {
                    links.Add(new Link()
                        {
                            Field = (PropertyInfo)mem.Member,
                            Table = mem.Expression.Type,
                        }); 
                }
                if (mem.Expression.NodeType == ExpressionType.Parameter)
                {
                    links.Add(new Link()
                    {
                        Field = (PropertyInfo) mem.Member,
                        Table = mem.Expression.Type,
                    }); 
                }
                
            }
            return links;
        }
    }

}